package com.admin.web.controller;


import com.admin.web.controller.commen.BaseController;
import com.fegin.bean.admin.SysAgreement;
import com.fegin.bean.common.wangEditBean.WangEditResult;
import com.fegin.client.admin.SysAgreementFegin;
import com.util.aliOSS.OSSFather;
import com.util.aliOSS.OSSInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

/**
 * <p>
 * 系统协议 前端控制器
 * </p>
 *
 * @author 张子艺
 * @since 2019-01-22
 */
@Controller
@RequestMapping("/admin/sysAgreement")
public class SysAgreementController extends BaseController {
    @Autowired
    SysAgreementFegin sysAgreementFegin;

    /**
     * 协议主页面
     *
     * @return
     */
    @RequestMapping(value = "/main.html", method = RequestMethod.GET)
    public String main(Model model) {
        model.addAttribute("agreenment",sysAgreementFegin.findAgreement());
        return "system/agreement";
    }

    /**
     * 协议上传图片
     *
     * @return
     */
    @RequestMapping(value = "/upload")
    @ResponseBody
    public WangEditResult main(HttpServletRequest request, @RequestParam("file") MultipartFile file) {
        OSSInterface ossInterface = new OSSInterface();
        WangEditResult wangEditResult = new WangEditResult();
        //上传文件到OSS
        try {
            String url = ossInterface.uploadImage(file.getOriginalFilename(), file.getInputStream(), OSSFather.bucketName_user);
            wangEditResult.setErrno(0);
            wangEditResult.setData(new String[]{url});
        } catch (IOException e) {
            e.printStackTrace();
            wangEditResult.setErrno(1);
            wangEditResult.setMsg("上传失败");
        }
        return wangEditResult;
    }

    /**
     * 协议保存
     *
     * @return
     */
    @RequestMapping(value = "/insertAgreement")
    @ResponseBody
    public Map<String, Object> insertAgreement(SysAgreement sysAgreement) {
        sysAgreement.setUpdateTime(System.currentTimeMillis());
       return sysAgreementFegin.insertImage(sysAgreement);
    }


}
